package com.avic.rong.course.activity;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.course.adapter.PublishListAdapter;
import com.avic.rong.course.bean.PublishGYBean;
import com.avic.rong.course.bean.PublishXQBean;
import com.avic.rong.course.showapi.PublisgApi;
import com.avic.rong.userOrder.adapter.MineOrderListAdapter;
import com.avic.rong.userOrder.bean.AllOrderListBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class PublishFragment extends BaseFragment implements OnRefreshListener, OnLoadMoreListener {


    @BindView(R.id.order_list)
    RecyclerView orderListRecy;

    private LoadingLayout loading_layout;
    Unbinder unbinder;
    private PublishListAdapter publishListAdapter;

    private String type;

    SmartRefreshLayout refreshLayout;
    private String orderType;

    //列表更新
    private int LIST_UPDATE = 1000;


    private LinearLayout ll_order_no_data;
    private String fabuname;

    @Override
    protected void initView() {
        refreshLayout = fragmentView.findViewById(R.id.refreshLayout);
        loading_layout = fragmentView.findViewById(R.id.loading_layout);
        ll_order_no_data = fragmentView.findViewById(R.id.ll_order_no_data);


//        EventBus.getDefault().register(this);
        Bundle arguments = getArguments();
        type = (String) arguments.get("ORDER_TYPE");
        fabuname = PublishCardActivity.Fabuname;
        if (TextUtils.isEmpty(type)) {
            return;
        }
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        //这里根据判断去设置适配器和布局管理器
        orderListRecy.setLayoutManager(new LinearLayoutManager(getContext()));
        if (fabuname.equals("发布的需求")) {

        } else {

        }

        publishListAdapter = new PublishListAdapter(context);
        orderListRecy.setAdapter(publishListAdapter);
        initData();


    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
       /* if(isVisibleToUser){
            if (TextUtils.isEmpty(type))return;
            initData(orderType);
        }*/
    }

    private void initData() {

        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {

            Map<String, String> map = new HashMap<>();
            //订单类型0：全部、1：审核中、2：对接中、11：未通过、99：已关闭、

            if (type.equals("全部")) {
//                map.put("status", "0");
            } else if (type.equals("审核中")) {
                map.put("status", "1");
            } else if (type.equals("对接中")) {
                map.put("status", "2");
            } else if (type.equals("未通过")) {
                map.put("status", "11");
            } else if (type.equals("已关闭")) {
                map.put("status", "99");
            }
            map.put("pageNo", currentPageNumber + "");
            map.put("pageSize", "10");
            map.put("token", getSharedPreferencesUtil().getLogintoken());


            //需求写在上面  供应室下面  上面的map  不用动因为穿的参数是一样的就是接口不同，下面写接口传map就可以
            PublisgApi publisgApi = new PublisgApi((RxAppCompatActivity) getActivity(), simpleOnNextListener);
            publisgApi.setCache(false);
//            if (fabuname.equals("发布的需求")) {
            publisgApi.setMap(map, fabuname);
//            } else {
//                publisgApi.setMap(map, fabuname);
//            }
            HttpManager.getInstance().doHttpDeal(publisgApi);

            loading_layout.setStatus(LoadingLayout.LOADING);
            loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
                @Override
                public void onReload(View v) {

                    loading_layout.setStatus(LoadingLayout.LOADING);
                }

                @Override
                public void onReturn(View v) {
                    getActivity().onBackPressed();
                }
            });
//            loading_layout.setStatus(LoadingLayout.LOADING);
            loading_layout.setStatus(LoadingLayout.SUCCESS);
        } else {
            loading_layout.setStatus(LoadingLayout.EMPTY);
        }
    }

    @Override
    protected void addListener() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_publish_list;
    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        isRefresh = false;
        currentPageNumber = currentPageNumber + 1;
        initData();

//        refreshLayout.finishLoadMore(2000);//传入false表示加载失败
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        isRefresh = true;
        currentPageNumber = 1;
        initData();
//        refreshLayout.finishRefresh(2000);//传入false表示刷新失败
    }


    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
//            if (isRefresh) {
//                listData.clear();
//            }

            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_1000)) {
                ToastTools.showToast(getContext(), subjects.getMsg());
                return;
            }
            if (fabuname.equals("发布的需求")) {
                List<PublishXQBean> data = (List<PublishXQBean>) subjects.getData();
                publishListAdapter.setListXQData(data,fabuname);
            } else {
                List<PublishGYBean> data = (List<PublishGYBean>) subjects.getData();
                publishListAdapter.setListGYData(data,fabuname);
            }

            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }

        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        EventBus.getDefault().unregister(this);
    }


}
